package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.QuarantineRequestDetailItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.ProdItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.QunCdItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class QuarantineRequestDetailThread extends BasicThread {

	public interface QuarantineRequestDetailThreadCallback {
		public void OnQuarantineRequestDetailCompleted(boolean isNetworkConntected
				, QuarantineRequestDetailItem quarantineItem, String message);
		public void OnQuarantineRequestDetailError(ErrorItem errorItem);
	}

	private String url;
	private QuarantineRequestDetailThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private String offCd, recNo;

	public QuarantineRequestDetailThread(Context context
			, QuarantineRequestDetailThreadCallback callback
			, String offCd, String recNo, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.offCd = offCd;
		this.recNo = recNo;
		this.appStatus = (AppStatus) context.getApplicationContext();
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String param = "";
		
		map.put(Const.SEARCHOFFCD, offCd);
		map.put(Const.SEARCHRECNO, recNo);
		param = parameterReturn(Const.REQ004, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnQuarantineRequestDetailCompleted(false, null, "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnQuarantineRequestDetailError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				callback.OnQuarantineRequestDetailError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayQuarantine = json.getJSONArray(Const.DATA);
				JSONObject quarantine = arrayQuarantine.getJSONObject(0);
				Log.i("quarantine","quarantine = " + quarantine);
				
				QuarantineRequestDetailItem quarantineItem = new QuarantineRequestDetailItem();
				
				ArrayList<ProdItem> arrayItems = new ArrayList<ProdItem>();
				JSONArray items	 	= json.getJSONArray(QuarantineRequestDetailItem.ITEMS);
				Log.i("items","items = " + items);
				for(int i = 0; i < items.length(); i++) {
					JSONObject itemObject = items.getJSONObject(i);
					ProdItem prodItem = new ProdItem();
					prodItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.OFFCD)));
					prodItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.RECNO)));
					prodItem.setRecSeq(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.RECSEQ)));
					prodItem.setTrfclsfCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.TRFCLSFCODE)));
					prodItem.setPrdlstCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.PRDLSTCODE)));
					prodItem.setPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.PRDLSTNM)));
					prodItem.setBotNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.BOTNM)));
					prodItem.setHsCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.HSCD)));
					prodItem.setItmQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.ITMQTY)));
					prodItem.setQunCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.QUNCD)));
					prodItem.setItmCnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.ITMCNT)));
					prodItem.setIsDb(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, ProdItem.ISDB)));
					arrayItems.add(prodItem);
				}
				quarantineItem.setItems(arrayItems);
				
				ArrayList<QunCdItem> arrayquns = new ArrayList<QunCdItem>();
				JSONArray qunitems	 	= json.getJSONArray(QuarantineRequestDetailItem.QUNCDLIST);
				for(int i = 0; i < qunitems.length(); i++) {
					JSONObject itemObject = qunitems.getJSONObject(i);
					QunCdItem qunItem = new QunCdItem();
					qunItem.setCode(appStatus.utilm.getStringJSONObject(itemObject, QunCdItem.CODE));
					qunItem.setCodeNm(appStatus.utilm.getStringJSONObject(itemObject, QunCdItem.CODENM));
					arrayquns.add(qunItem);
				}
				quarantineItem.setQunCdList(arrayquns);
				
				quarantineItem.setItemsResult(json.getString(QuarantineRequestDetailItem.ITEMSRESULT));
				quarantineItem.setValidflagNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VALIDFLAGNM)));
				quarantineItem.setTrnNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.TRNNM)));
				quarantineItem.setInsttCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.INSTTCD)));
				quarantineItem.setAllInsttNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.ALLINSTTNM)));
				quarantineItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.OFFCD)));
				quarantineItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.RECNO)));
				quarantineItem.setRegType(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.REGTYPE)));
				quarantineItem.setTsqno(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.TSQNO)));
				quarantineItem.setRceptDate(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.RCEPTDATE)));
				quarantineItem.setRceptDateHh(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.RCEPTDATEHH)));
				quarantineItem.setRceptDateMi(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.RCEPTDATEMI)));
				quarantineItem.setTraDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.TRADE)));
				quarantineItem.setWrhousngDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.WRHOUSNGDE)));
				quarantineItem.setDelayCheck(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.DELAYCHECK)));
				quarantineItem.setQrantHde(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.QRANTHDE)));
				quarantineItem.setRecTime(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.RECTIME)));
				//quarantineItem.setRecTimeList(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.RECTIMELIST));
				quarantineItem.setViewRecNamCdNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VIEWRECNAMCDNM)));
				quarantineItem.setViewRecNamCdAcnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VIEWRECNAMCDACNT)));
				quarantineItem.setTerDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.TERDE)));
				quarantineItem.setBoseNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.BOSENO)));
				quarantineItem.setInsPlace(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.INSPLACE)));
				quarantineItem.setElcQrantCrtfNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.ELCQRANTCRTFNO)));
				quarantineItem.setHeatReportNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.HEATREPORTNO)));
				quarantineItem.setObiwuTrgetAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.OBIWUTRGETAT)));
				quarantineItem.setTrnCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.TRNCD)));
				quarantineItem.setValidflag(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VALIDFLAG)));
				quarantineItem.setExpCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.EXPCD)));
				quarantineItem.setExpNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.EXPNM)));
				quarantineItem.setMetCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.METCD)));
				quarantineItem.setMetNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.METNM)));
				quarantineItem.setLoadNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.LOADNM)));
				quarantineItem.setLoadSp(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.LOADSP)));
				quarantineItem.setArivSp(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.ARIVSP)));
				quarantineItem.setCaseSt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.CASEST)));
				quarantineItem.setCaseCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.CASECD)));
				quarantineItem.setCaseNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.CASENM)));
				quarantineItem.setTotQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.TOTQTY)));
				quarantineItem.setPlItem(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.PLITEM)));
				quarantineItem.setCvodv(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.CVODV)));
				quarantineItem.setBlNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.BLNO)));
				quarantineItem.setHwaNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.HWANO)));
				quarantineItem.setNatCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.NATCD)));
				quarantineItem.setNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.NATNM)));
				quarantineItem.setOrgNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.ORGNATNM)));
				quarantineItem.setOrgAreCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.ORGARECD)));
				quarantineItem.setOrgAreNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.ORGARENM)));
				quarantineItem.setPasNatCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.PASNATCD)));
				quarantineItem.setPasNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.PASNATNM)));
				quarantineItem.setDelayAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.DELAYAT)));
				quarantineItem.setVenCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENCD)));
				quarantineItem.setVenNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENNM)));
				quarantineItem.setVenComlice(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENCOMLICE)));
				quarantineItem.setVenRep(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENREP)));
				quarantineItem.setVenRnAdres(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENRNADRES)));
				quarantineItem.setVenRnAdresDetail(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENRNADRESDETAIL)));
				quarantineItem.setVenResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENRESNO)));
				quarantineItem.setVenTelNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENTELNO)));
				quarantineItem.setVenCelphn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENCELPHN)));
				quarantineItem.setVenSmsChk(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENSMSCHK)));
				quarantineItem.setVenZipCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENZIPCODE)));
				quarantineItem.setVenLegaldongCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENLEGALDONGCODE)));
				quarantineItem.setVenLegaldongAdres(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENLEGALDONGADRES)));
				quarantineItem.setVenLegaldongAdresDetail(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENLEGALDONGADRESDETAIL)));
				quarantineItem.setVenRnCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VENRNCODE)));
				quarantineItem.setVen2RepNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VEN2REPNM)));
				quarantineItem.setVen2Nm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VEN2NM)));
				quarantineItem.setVen2Adres(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VEN2ADRES)));
				quarantineItem.setAgentNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.AGENTNM)));
				quarantineItem.setAgentTelNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.AGENTTELNO)));
				quarantineItem.setAgentCelphn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.AGENTCELPHN)));
				quarantineItem.setAgentSmsChk(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.AGENTSMSCHK)));
				quarantineItem.setAgentFaxno(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.AGENTFAXNO)));
				quarantineItem.setAgentEmail(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.AGENTEMAIL)));
				quarantineItem.setAgentReq(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.AGENTREQ)));
				quarantineItem.setPcyn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.PCYN)));
				quarantineItem.setDiplanyn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.DIPLANYN)));
				quarantineItem.setDiagreeyn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.DIAGREEYN)));
				quarantineItem.setEcrequestyn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.ECREQUESTYN)));
				quarantineItem.setLmoYn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.LMOYN)));
				quarantineItem.setMsgId(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.MSGID)));
				quarantineItem.setEndDiv(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.ENDDIV)));
				quarantineItem.setFileId(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.FILEID)));
				quarantineItem.setReqstProgrsSt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.REQSTPROGRSST)));
				quarantineItem.setViewReqstProgrsSt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.VIEWREQSTPROGRSST)));
				quarantineItem.setDocScrtySe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.DOCSCRTYSE)));
				quarantineItem.setScrtyCnfirmNamCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.SCRTYCNFIRMNAMCD)));
				quarantineItem.setScrtyCnfirmNamCdNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.SCRTYCNFIRMNAMCDNM)));
				quarantineItem.setScrtyCnfirmNamCdAcnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.SCRTYCNFIRMNAMCDACNT)));
				quarantineItem.setMtCloseEndDiv(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.MTCLOSEENDDIV)));
				quarantineItem.setProgrsNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(quarantine, QuarantineRequestDetailItem.PROGRSNM)));
				
				callback.OnQuarantineRequestDetailCompleted(true, quarantineItem, mResult);
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
	}
}
